<template>
  <Panel>
    <template #title>商品修改</template>
    <template #content>
      <el-form
        :model="edilData"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="edilData.name"></el-input>
        </el-form-item>
        <el-form-item label="所属分类" prop="category">
          <el-select v-model="edilData.category" placeholder="请选择商品分类">
            <el-option
              v-for="item in categoryList"
              :key="item.cateName"
              :label="item.cateName"
              :value="item.cateName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品图片" prop="imgUrl">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="edilData.imgUrl"
              :src="
                'http://127.0.0.1:5000/upload/imgs/goods_img/' + edilData.imgUrl
              "
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input-number
            v-model="edilData.price"
            :min="1"
            :max="9999"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="商品描述" prop="goodsDesc">
          <el-input type="textarea" v-model="edilData.goodsDesc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-waves type="primary" @click="submitForm('ruleForm')"
            >立即修改</el-button
          >
          <el-button v-waves @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
  </Panel>
</template>

<script>
import { allCate, editGoods } from '@/api/good'
import local from '@/utils/local'

export default {
  data() {
    return {
      edilData: {},
      categoryList: [],
      rules: {
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        imgUrl: [
          { required: true, message: '请选择商品图片', trigger: 'change' }
        ],
        category: [
          { required: true, message: '请选择商品分类', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请选择商品价格', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getAllCate()
    this.edilData = JSON.parse(local.get('goods'))
    this.edilData.imgUrl = JSON.parse(local.get('goods')).imgUrl
  },
  methods: {
    async getAllCate() {
      const { categories } = await allCate()
      this.categoryList = categories
    },
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          await editGoods(this.edilData)
          setTimeout(() => this.$router.push('/goods/list'), 1000)
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleAvatarSuccess(res, file) {
      this.edilData.imgUrl = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isLt2M
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
/deep/.el-input,
.el-input-number,
/deep/textarea {
  width: 200px;
}
</style>
